<?php

// 上传游戏结果
require_once 'common/conn.php';
if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];
}
if (isset($_GET['opponent'])) {
    $opponent = $_GET['opponent'];
}
if (isset($_GET['gametime'])) {
    $gametime = $_GET['gametime'];
}
// 1 胜利 -1 失败 0 平局
if (isset($_GET['result'])) {
    $result = $_GET['result'];
}
if (isset($_GET['gold'])) {
    $gold = $_GET['gold'];
}
$result_array = array();
if (isset($usercode) && isset($opponent) && isset($gametime) && isset($result) && isset($gold)) {
    $db = new DBC();
    $sql = "SELECT deviceid FROM t_device WHERE devicecode = '" . $usercode . "'";
    $data = $db->fetchData($sql);
    if ($data != false && count($data) > 0) {
        $deviceid = $data[0]['deviceid'];
        $table = "t_game_record_" . $deviceid;
        $sql = "SHOW TABLES LIKE '" . $table . "'";
        $data = $db->fetchData($sql);
        $is_table_exist = false;
        if ($data != false && count($data) > 0) {
            $is_table_exist = true;
        }
        if (!$is_table_exist) {
            $sql = "CREATE TABLE `" . $table . "` (
            `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
            `usercode` varchar(50) DEFAULT NULL COMMENT 'uid,自动生成',
            `opponent` varchar(50) DEFAULT NULL COMMENT '对手',
            `gold` int(11) DEFAULT '0' COMMENT '获得金币数量',
            `gametime` datetime DEFAULT NULL COMMENT '游戏时间',
            `duration` int(11) DEFAULT '0' COMMENT '持续时间',
            `result` int(11) DEFAULT NULL COMMENT '1 胜利 -1 失败 0 平局',
            `right` int(11) DEFAULT '0' COMMENT '正确数',
            `wrong` int(11) DEFAULT '0' COMMENT '错误数',
            PRIMARY KEY (`id`)
            ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
            $db->execute($sql);
        }
        $sql = "INSERT INTO " . $table . "(usercode,opponent,gametime,result,gold) VALUES('" . $usercode . "','" . $opponent . "','" . $gametime . "'," . $result . "," . $gold . ")";
        if ($db->execute($sql)) {
            $result_array["errorNo"] = "0";
            $result_array["errorMsg"] = "";
        } else {
            $result_array["errorNo"] = "1";
            $result_array["errorMsg"] = "添加数据错误";
        }
    } else {
        $result_array["errorNo"] = "1";
        $result_array["errorMsg"] = "UID:" . $usercode . " 不存在.";
    }
}
echo json_encode($result_array);
?>
